home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / HTetris / htetris.jar / tetris / Options.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-07-19  |  6.9 KB  |  215 lines

  1. package tetris;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import javax.microedition.lcdui.ChoiceGroup;
  8. import javax.microedition.lcdui.Command;
  9. import javax.microedition.lcdui.CommandListener;
  10. import javax.microedition.lcdui.Display;
  11. import javax.microedition.lcdui.Displayable;
  12. import javax.microedition.lcdui.Form;
  13. import javax.microedition.lcdui.Gauge;
  14. import javax.microedition.lcdui.Image;
  15. import javax.microedition.lcdui.Item;
  16. import javax.microedition.lcdui.ItemStateListener;
  17. import javax.microedition.rms.RecordStore;
  18.  
  19. public class Options extends Form implements CommandListener, ItemStateListener {
  20.    static int keyLeft;
  21.    static int keyRight;
  22.    static int keyUp;
  23.    static int keyDown;
  24.    static boolean optSounds = false;
  25.    static boolean optPreview = true;
  26.    static boolean optDefaultKeys = true;
  27.    static boolean optProgressive = true;
  28.    static int startLevel = 0;
  29.    static int garbageLevel = 0;
  30.    static RecordStore optionsRS;
  31.    static ChoiceGroup opt1;
  32.    static ChoiceGroup opt2;
  33.    static Gauge opt3;
  34.    static Gauge opt4;
  35.  
  36.    public Options() {
  37.       super("Options");
  38.  
  39.       try {
  40.          this.jbInit();
  41.       } catch (Exception e) {
  42.          ((Throwable)e).printStackTrace();
  43.       }
  44.  
  45.    }
  46.  
  47.    protected static void openStore() {
  48.       try {
  49.          optionsRS = RecordStore.openRecordStore("options", true);
  50.       } catch (Exception e) {
  51.          ((Throwable)e).printStackTrace();
  52.       }
  53.  
  54.    }
  55.  
  56.    protected static void closeStore() {
  57.       try {
  58.          optionsRS.closeRecordStore();
  59.       } catch (Exception e) {
  60.          ((Throwable)e).printStackTrace();
  61.       }
  62.  
  63.    }
  64.  
  65.    private void jbInit() throws Exception {
  66.       ((Displayable)this).setCommandListener(this);
  67.       ((Form)this).setItemStateListener(this);
  68.       ((Displayable)this).addCommand(new Command("OK", 2, 1));
  69.       openStore();
  70.       readOptions();
  71.       closeStore();
  72.       opt1 = new ChoiceGroup("", 2, new String[]{"Preview", "Progressive"}, (Image[])null);
  73.       ((Form)this).append(opt1);
  74.       opt2 = new ChoiceGroup("", 2, new String[]{"Default keys"}, (Image[])null);
  75.       ((Form)this).append(opt2);
  76.       opt3 = new Gauge("Starting level", true, 4, startLevel);
  77.       ((Form)this).append(opt3);
  78.       opt4 = new Gauge("Garbage level", true, 4, garbageLevel);
  79.       ((Form)this).append(opt4);
  80.       opt1.setSelectedFlags(new boolean[]{optPreview, optProgressive});
  81.       opt2.setSelectedFlags(new boolean[]{optDefaultKeys});
  82.    }
  83.  
  84.    public void commandAction(Command command, Displayable displayable) {
  85.       if (command.getCommandType() == 3) {
  86.          Display.getDisplay(TetrisMIDlet.instance).setCurrent(this);
  87.       } else if (command.getCommandType() == 4) {
  88.          TetrisMIDlet.mainMenu.redefineKeys(this);
  89.       } else if (command.getCommandType() == 2) {
  90.          optPreview = opt1.isSelected(0);
  91.          optDefaultKeys = opt2.isSelected(0);
  92.          optProgressive = opt1.isSelected(1);
  93.          startLevel = opt3.getValue();
  94.          garbageLevel = opt4.getValue();
  95.          openStore();
  96.          saveOptions();
  97.          closeStore();
  98.          Display.getDisplay(TetrisMIDlet.instance).setCurrent(TetrisMIDlet.mainMenu);
  99.       }
  100.  
  101.    }
  102.  
  103.    public void itemStateChanged(Item item) {
  104.       if (item == opt2 && !opt2.isSelected(0)) {
  105.          Form ck = new Form("Current keys");
  106.          optDefaultKeys = false;
  107.          ck.append(currentKeys(false));
  108.          ((Displayable)ck).addCommand(new Command("Redefine", 4, 1));
  109.          ((Displayable)ck).addCommand(new Command("OK", 3, 2));
  110.          ((Displayable)ck).setCommandListener(this);
  111.          Display.getDisplay(TetrisMIDlet.instance).setCurrent(ck);
  112.       }
  113.  
  114.    }
  115.  
  116.    public static void saveOptions() {
  117.       try {
  118.          byte[] b = new byte[]{(byte)(optSounds ? 1 : 0), (byte)(optPreview ? 1 : 0), (byte)(optDefaultKeys ? 1 : 0), (byte)(optProgressive ? 1 : 0), (byte)startLevel, (byte)garbageLevel};
  119.          if (optionsRS.getNumRecords() > 0) {
  120.             optionsRS.setRecord(1, b, 0, b.length);
  121.          } else {
  122.             optionsRS.addRecord(b, 0, b.length);
  123.          }
  124.       } catch (Exception e) {
  125.          ((Throwable)e).printStackTrace();
  126.       }
  127.  
  128.    }
  129.  
  130.    public static void saveKeys() {
  131.       openStore();
  132.  
  133.       try {
  134.          ByteArrayOutputStream baos = new ByteArrayOutputStream();
  135.          DataOutputStream dos = new DataOutputStream(baos);
  136.          dos.writeInt(keyLeft);
  137.          dos.writeInt(keyRight);
  138.          dos.writeInt(keyUp);
  139.          dos.writeInt(keyDown);
  140.          byte[] b = baos.toByteArray();
  141.          if (optionsRS.getNumRecords() > 1) {
  142.             optionsRS.setRecord(2, b, 0, b.length);
  143.          } else {
  144.             saveOptions();
  145.             optionsRS.addRecord(b, 0, b.length);
  146.          }
  147.       } catch (Exception e) {
  148.          ((Throwable)e).printStackTrace();
  149.       }
  150.  
  151.       closeStore();
  152.    }
  153.  
  154.    public static void readOptions() {
  155.       try {
  156.          if (optionsRS.getNumRecords() > 0) {
  157.             byte[] b = optionsRS.getRecord(1);
  158.             optSounds = b[0] == 1;
  159.             optPreview = b[1] == 1;
  160.             optDefaultKeys = b[2] == 1;
  161.             optProgressive = b[3] == 1;
  162.             startLevel = b[4];
  163.             garbageLevel = b[5];
  164.          }
  165.       } catch (Exception e) {
  166.          ((Throwable)e).printStackTrace();
  167.       }
  168.  
  169.    }
  170.  
  171.    public static boolean readKeys() {
  172.       openStore();
  173.  
  174.       try {
  175.          if (optionsRS.getNumRecords() > 1) {
  176.             byte[] b = optionsRS.getRecord(2);
  177.             ByteArrayInputStream bais = new ByteArrayInputStream(b);
  178.             DataInputStream dis = new DataInputStream(bais);
  179.             keyLeft = dis.readInt();
  180.             keyRight = dis.readInt();
  181.             keyUp = dis.readInt();
  182.             keyDown = dis.readInt();
  183.             closeStore();
  184.             boolean var3 = true;
  185.             return var3;
  186.          }
  187.       } catch (Exception e) {
  188.          ((Throwable)e).printStackTrace();
  189.       }
  190.  
  191.       closeStore();
  192.       return false;
  193.    }
  194.  
  195.    static String currentKeys(boolean b) {
  196.       String k1;
  197.       String k2;
  198.       String k3;
  199.       String k4;
  200.       if (optDefaultKeys) {
  201.          k1 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(1));
  202.          k2 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(6));
  203.          k3 = TetrisMIDlet.gameScreen.getKeyName(53);
  204.          k4 = TetrisMIDlet.gameScreen.getKeyName(TetrisMIDlet.gameScreen.getKeyCode(2));
  205.       } else {
  206.          k1 = TetrisMIDlet.gameScreen.getKeyName(keyLeft);
  207.          k2 = TetrisMIDlet.gameScreen.getKeyName(keyRight);
  208.          k3 = TetrisMIDlet.gameScreen.getKeyName(keyUp);
  209.          k4 = TetrisMIDlet.gameScreen.getKeyName(keyDown);
  210.       }
  211.  
  212.       return new String(String.valueOf(String.valueOf((new StringBuffer(String.valueOf(String.valueOf(b ? "Current keys:\n" : "")))).append(k1).append(" - left\n").append(k2).append(" - right\n").append(k3).append(" - rotate\n").append(k4).append(" - drop"))));
  213.    }
  214. }
  215.